﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class FightCalcResult
{
     public float attack;
     public float defense;

     // 攻击范围
     public float attackR;
}
public class CharactorCtrl : MonoBehaviour
{
     public UserInfoData userData;
     public AnimData animData;
     public FightData fightData;

     public BuffTimeLine buffTimeLine;
     public SkillTimeLine skillTimeLine;
     public bool isSelf;
     public void Init(int id, bool isSelf)
     {
          this.isSelf = isSelf;
          this.userData.Init(id);
          this.animData.Init(this);
          this.fightData.Init(ref this.userData);
          this.buffTimeLine.Init();
          this.skillTimeLine.Init();
          SetState(CharactorState.Idle);
     }

     /// <summary>
     /// 切换动画状态
     /// </summary>
     /// <param name="state"></param>
     public void SetState(CharactorState state)
     {
          this.animData.SetState(state);
     }

     public void StartSkill(int skillId)
     {
          Action callback = () =>
          {
               animData.SetState(CharactorState.Idle);
          };
          if (!skillTimeLine.StartSkill(this, skillId,callback))
          {
                animData.SetState(CharactorState.Attack);
          }
           
     }

     public void StartBuff(int buffId)
     {
          if (buffTimeLine.StartBuff(buffId))
          {
               EventMgr.Instance.Emit((int)EventTypes.UI, UIEvents.BuffOpened);
          }
     }

     public void CalcFightBuff(string funcName, FightCalcResult result)
     {
          this.buffTimeLine.CalcAllBuffsWithFuncName(funcName, result);
     }

     public void OnLoseHp(int loseHp)
     {
          fightData.hp -= loseHp;
          if (fightData.hp <= 0)
          {
               fightData.hp = 0;
               SetState(CharactorState.Died);
          }
          Debug.Log($"角色{this.userData.charactorId} 失去{loseHp}点hp，剩余{fightData.hp}点hp ");
          EventMgr.Instance.Emit((int)EventTypes.UI, isSelf ? UIEvents.SyncSelfHp:UIEvents.SyncEnemyHp, fightData.hp);
     }

     public void Update()
     {
          buffTimeLine.OnUpdate(Time.deltaTime);
          skillTimeLine.OnUpdate(Time.deltaTime);
     }
}
